package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.repository.RegionRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
//@Transactional  暂时先别管事务
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private DestinationRepository repository;

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        repository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public List<Destination> queryByRegionId(String rid) {
        //1.通过rid获取关联的目的的id集合
        Region region = regionService.get(rid);
        List<String> ids = region.getRefIds();
        //2.通过id集合查询目的的对象集合
        return repository.findByIdIn(ids);
    }
}
